/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Lop;

import java.io.File;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.TypeInfo;
import org.w3c.dom.UserDataHandler;


/**
 *
 * @author Administrator
 */
public class OConnection {

    private String Type;
    private String ServerName;
    private String ServiceName;
    private String UserName;
    private String Password;
    private String Port;
    private String FileName;

    public OConnection(){
        this.Type = "";
        this.ServerName = "";
        this.ServiceName = "";
        this.UserName = "";
        this.Password = "";
        this.Port = "";
        this.FileName = "";
    }
    
    public String getType() {
        return Type;
    }

    public void setType(String Type) {
        this.Type = Type;
    }
    
    public String getServerName(){
        return ServerName;
    }
    
    public String getServiceName(){
        return ServiceName;
    }
    
    public String getUserName(){
        return UserName;
    }
    
    public String getPassword(){
        return Password;
    }
    
    public String getPort(){
        return Port;
    }
    
    public void setServerName(String serverName){
        this.ServerName = serverName;
    }
    
    public void setServiceName(String serviceName){
        this.ServiceName = serviceName;
    }
    
    public void setUserName(String userName){
        this.UserName = userName;
    }
    
    public void setPassword(String password){
        this.Password = password;
    }
    
    public void setPort(String port){
        this.Port = port;
    }
    
    public String getFileName() {
        return FileName;
    }

    public void setFileName(String FileName) {
        this.FileName = FileName;
    }
    
    public Connection createConnection ()
    throws SQLException, ClassNotFoundException{
        //initial oracle driver
        Class.forName("oracle.jdbc.OracleDriver");
        String url = "jdbc:oracle:thin:@//" + ServerName + ":" + Port + "/" + ServiceName;
        Connection connObject = DriverManager.getConnection(url, UserName, Password);
        return connObject;
    }
    public void XMLRead() throws Exception{
        
        //The two lines below are just for getting an
        //instance of DocumentBuilder which we use
        //for parsing XML data
        
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        //Here we do the actual parsing
        Document doc = builder.parse(new File("src/conn/cauhinh.xml"));

        //Here we get the root element of XML and print out
        //the value of its 'testAttr' attribute
        Element rootElement = doc.getDocumentElement();
               
        Type = getTextValue(Type, rootElement, "Type");
        ServerName = getTextValue(ServerName, rootElement, "ServerName");
        ServiceName = getTextValue(ServiceName, rootElement, "ServiceName");
        UserName = getTextValue(UserName, rootElement, "UserName");
        Password = getTextValue(Password, rootElement, "Password");
        Port = getTextValue(Port, rootElement, "Port");
        FileName = getTextValue(FileName, rootElement, "FileName");
    }
     private String getTextValue(String def, Element doc, String tag)
    {
        String value = def;
        NodeList nl;
        nl = doc.getElementsByTagName(tag);
        if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
            value = nl.item(0).getFirstChild().getNodeValue();
        }
        return value;
    }
    public void XMLWrite(String type, String serverName, String serviceName, String userName, String password, String port, String fileName) throws Exception{
        
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Here instead of parsing an existing document we want to
        // create a new one.
        Document doc = builder.newDocument();

        // This creates a new tag named 'testElem' inside
        // the document and sets its data to 'TestContent'
         Element cauhinh = doc.createElement("root");
         doc.appendChild(cauhinh);
        
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "Type", type));
        
        //create ServerName element
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "ServerName", serverName));
 
        //create ServiceName element
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "ServiceName", serviceName));
 
        //create UserName element
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "UserName", userName));
 
        //create Password element
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "Password", password));
 
        //create Port element
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "Port", port));
        
        cauhinh.appendChild(getEmployeeElements(doc, cauhinh, "FileName", fileName));
        
        this.Type = type;
        this.ServerName = serverName;
        this.ServiceName = serviceName;
        this.UserName = userName;
        this.Password = password;
        this.Port = port;
        this.FileName = fileName;
         
        DOMSource source = new DOMSource(doc);

        // PrintStream will be responsible for writing
        // the text data to the file
        PrintStream ps = new PrintStream("src/conn/cauhinh.xml");
        StreamResult result = new StreamResult(ps);

        // Once again we are using a factory of some sort,
        // this time for getting a Transformer instance,
        // which we use to output the XML
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        // The actual output to a file goes here
        transformer.transform(source, result);
    }    
   
    private Node getEmployeeElements(Document doc, Element cauhinh, String name, String value) {
        Element node = doc.createElement(name);
        node.appendChild(doc.createTextNode(value));
        return node;
    }
}
